﻿using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;

namespace HtmlMaker
{
    public class GetSystemIco
    {
        public static Icon GetIconByFileName(string fileName)
        {
            if ((fileName == null) || fileName.Equals(string.Empty))
            {
                return null;
            }
            if (!File.Exists(fileName))
            {
                return null;
            }
            SHFILEINFO psfi = new SHFILEINFO();
            Win32.SHGetFileInfo(fileName, 0, ref psfi, (uint)Marshal.SizeOf(psfi), 0x100);
            return Icon.FromHandle(psfi.hIcon);
        }


    }

    [StructLayout(LayoutKind.Sequential)]
    public struct SHFILEINFO
    {
        public IntPtr hIcon;
        public IntPtr iIcon;
        public uint dwAttributes;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
        public string szDisplayName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
        public string szTypeName;
    }

    internal class Win32
    {
        // Fields
        public const uint SHGFI_ICON = 0x100;
        public const uint SHGFI_LARGEICON = 0;
        public const uint SHGFI_SMALLICON = 1;

        // Methods
        public Win32() { }
        [DllImport("shell32.dll")]
        public static extern uint ExtractIconEx(string lpszFile, int nIconIndex, int[] phiconLarge, int[] phiconSmall, uint nIcons);
        [DllImport("shell32.dll")]
        public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
    }
}
